package com.screen.screendisplay.controller;

import com.screen.screendisplay.common.pojo.ApiResponse;
import com.screen.screendisplay.service.coal.CoalProdoperOutputService;
import com.screen.screendisplay.service.coal.CoalProdoperOsvService;
import com.screen.screendisplay.service.coal.CoalProdoperStockService;
import com.screen.screendisplay.service.coal.CoalProdoperSalesService;
import com.screen.screendisplay.service.coal.CoalProdoperTransportService;
import com.screen.screendisplay.service.coal.CoalProdoperSalesStructureService;
import com.screen.screendisplay.service.coal.CoalOverviewItemService;
import com.screen.screendisplay.service.coal.CoalOpermonEquipUseService;
import com.screen.screendisplay.service.coal.CoalOpermonEquipFaultService;
import com.screen.screendisplay.service.coal.CoalOpermonRtOutputService;
import com.screen.screendisplay.service.coal.CoalOpermonRtEffectService;
import com.screen.screendisplay.service.coal.CoalOpermonRtSlopeAlarmService;
import com.screen.screendisplay.service.coal.CoalSafetyenvRestorationService;
import com.screen.screendisplay.service.coal.CoalSafetyenvRiskDealService;
import com.screen.screendisplay.service.coal.CoalSafetyenvAlarmService;
import com.screen.screendisplay.vo.coal.CoalOutputVO;
import com.screen.screendisplay.vo.coal.CoalOutputUnitsVO;
import com.screen.screendisplay.vo.coal.CoalOutputTrendVO;
import com.screen.screendisplay.vo.coal.CoalStrippingVO;
import com.screen.screendisplay.vo.coal.CoalStrippingTrendVO;
import com.screen.screendisplay.vo.coal.CoalStrippingUnitsVO;
import com.screen.screendisplay.vo.coal.CoalInventoryTrendVO;
import com.screen.screendisplay.vo.coal.CoalInventoryUnitsVO;
import com.screen.screendisplay.vo.coal.CoalTransportVO;
import com.screen.screendisplay.vo.coal.CoalSalesStructureVO;
import com.screen.screendisplay.vo.coal.CoalSalesVO;
import com.screen.screendisplay.vo.coal.CoalTopIndicatorsVO;
import com.screen.screendisplay.vo.coal.CoalEquipmentUsageVO;
import com.screen.screendisplay.vo.coal.CoalEquipmentFaultVO;
import com.screen.screendisplay.vo.coal.CoalProductionOutputVO;
import com.screen.screendisplay.vo.coal.CoalProductionEfficiencyVO;
import com.screen.screendisplay.vo.coal.CoalSlopeAlertVO;
import com.screen.screendisplay.vo.coal.CoalEcoRestorationVO;
import com.screen.screendisplay.vo.coal.CoalWasteDisposalVO;
import com.screen.screendisplay.vo.coal.CoalSafetyAlertVO;
import com.screen.screendisplay.vo.coal.CoalMineIntroVO;
import com.screen.screendisplay.vo.coal.CoalBasicInfoVO;
import com.screen.screendisplay.vo.coal.CoalTransportDetailVO;
import com.screen.screendisplay.vo.coal.CoalUndergroundPersonnelVO;
import com.screen.screendisplay.vo.coal.CoalSafetyEnvVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 能源消费表 前端控制器
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Tag(name = "煤炭产业", description = "煤炭产业")
@RestController
@RequestMapping("/coal")
public class CoalController {

    @Autowired
    private CoalProdoperOutputService coalProdoperOutputService;
    
    @Autowired
    private CoalProdoperOsvService coalProdoperOsvService;
    
    @Autowired
    private CoalProdoperStockService coalProdoperStockService;
    
    @Autowired
    private CoalProdoperSalesService coalProdoperSalesService;
    
    @Autowired
    private CoalProdoperTransportService coalProdoperTransportService;
    
    @Autowired
    private CoalProdoperSalesStructureService coalProdoperSalesStructureService;
    
    @Autowired
    private CoalOverviewItemService coalOverviewItemService;
    
    @Autowired
    private CoalOpermonEquipUseService coalOpermonEquipUseService;
    
    @Autowired
    private CoalOpermonEquipFaultService coalOpermonEquipFaultService;
    
    @Autowired
    private CoalOpermonRtOutputService coalOpermonRtOutputService;
    
    @Autowired
    private CoalOpermonRtEffectService coalOpermonRtEffectService;
    
    @Autowired
    private CoalOpermonRtSlopeAlarmService coalOpermonRtSlopeAlarmService;
    
    @Autowired
    private CoalSafetyenvRestorationService coalSafetyenvRestorationService;
    
    @Autowired
    private CoalSafetyenvRiskDealService coalSafetyenvRiskDealService;
    
    @Autowired
    private CoalSafetyenvAlarmService coalSafetyenvAlarmService;


    @Operation(summary = "生产经营-煤炭产量-查询年及月产量完成情况", description = "生产经营-煤炭产量-查询年及月产量完成情况")
    @GetMapping(value = "/coalproduct/operation/coaloutput")
    public ApiResponse<CoalOutputVO> getCoalOutput(@RequestParam(value = "year", required = false) Integer year) {
        // 调用Service方法获取煤炭产量数据
        CoalOutputVO vo = coalProdoperOutputService.getCoalOutput(year);
        return ApiResponse.succeed(vo);
    }

    @Operation(summary = "生产经营-煤炭产量-各单位煤炭产量完成情况", description = "生产经营-煤炭产量-各单位煤炭产量完成情况")
    @GetMapping(value = "/coalproduct/operation/coaloutput/units")
    public ApiResponse<List<CoalOutputUnitsVO>> getCoalOutputByUnits(@RequestParam(value = "year", required = false) Integer year) {
        // 调用Service方法获取各单位煤炭产量数据
        List<CoalOutputUnitsVO> resultList = coalProdoperOutputService.getCoalOutputByUnits(year);
        return ApiResponse.succeed(resultList);
    }

    @Operation(summary = "生产经营-煤炭产量-煤炭产量完成趋势", description = "生产经营-煤炭产量-煤炭产量完成趋势")
    @GetMapping(value = "/coalproduct/operation/coaloutput/trend")
    public ApiResponse<List<CoalOutputTrendVO>> getCoalOutputTrend(@RequestParam(value = "year", required = false) Integer year) {
        // 调用Service方法获取煤炭产量趋势数据
        List<CoalOutputTrendVO> resultList = coalProdoperOutputService.getCoalOutputTrend(year);
        return ApiResponse.succeed(resultList);
    }

    @Operation(summary = "生产经营-土岩玻璃量-土岩剥离量完成情况", description = "生产经营-土岩玻璃量-土岩剥离量完成情况")
    @GetMapping(value = "/coalproduct/operation/stripping")
    public ApiResponse<CoalStrippingVO> getStripping(@RequestParam(value = "year", required = false) Integer year) {
        // 调用Service方法获取土岩剥离量数据
        CoalStrippingVO vo = coalProdoperOsvService.getStripping(year);
        return ApiResponse.succeed(vo);
    }

    @Operation(summary = "生产经营-土岩玻璃量-各单位土岩剥离量完成情况", description = "生产经营-土岩玻璃量-各单位土岩剥离量完成情况")
    @GetMapping(value = "/coalproduct/operation/stripping/units")
    public ApiResponse<List<CoalStrippingUnitsVO>> getStrippingByUnits(@RequestParam(value = "year", required = false) Integer year) {
        // 调用Service方法获取各单位土岩剥离量数据
        List<CoalStrippingUnitsVO> resultList = coalProdoperOsvService.getStrippingByUnits(year);
        return ApiResponse.succeed(resultList);
    }

    @Operation(summary = "生产经营-土岩玻璃量-土岩剥离量完成趋势", description = "生产经营-土岩玻璃量-土岩剥离量完成趋势")
    @GetMapping(value = "/coalproduct/operation/stripping/trend")
    public ApiResponse<List<CoalStrippingTrendVO>> m6(@RequestParam(value = "year", required = false) Integer year) {
        // 调用Service方法获取土岩剥离量趋势数据
        List<CoalStrippingTrendVO> resultList = coalProdoperOsvService.getStrippingTrend(year);
        return ApiResponse.succeed(resultList);
    }

    @Operation(summary = "生产经营-煤炭库存-煤炭库存量趋势", description = "生产经营-煤炭库存-煤炭库存量趋势")
    @GetMapping(value = "/coalproduct/operation/inventory/trend")
    public ApiResponse<List<CoalInventoryTrendVO>> getInventoryTrend(@RequestParam(value = "year", required = false) Integer year) {
        // 调用Service方法获取煤炭库存量趋势数据
        List<CoalInventoryTrendVO> resultList = coalProdoperStockService.getInventoryTrend(year);
        return ApiResponse.succeed(resultList);
    }

    @Operation(summary = "生产经营-煤炭库存-各单位煤炭库存情况", description = "生产经营-煤炭库存-各单位煤炭库存情况")
    @GetMapping(value = "/coalproduct/operation/inventory/units")
    public ApiResponse<List<CoalInventoryUnitsVO>> getInventoryByUnits(@RequestParam(value = "year", required = false) Integer year) {
        // 调用Service方法获取各单位煤炭库存数据
        List<CoalInventoryUnitsVO> resultList = coalProdoperStockService.getInventoryByUnits(year);
        return ApiResponse.succeed(resultList);
    }


    @Operation(summary = "生产经营-煤炭销售-煤炭销量完成情况", description = "生产经营-煤炭销售-煤炭销量完成情况")
    @GetMapping(value = "/coalproduct/operation/sales")
    public ApiResponse<CoalSalesVO> getCoalSales(@RequestParam(value = "year", required = false) Integer year) {
        // 调用Service方法获取煤炭销量数据
        CoalSalesVO vo = coalProdoperSalesService.getCoalSales(year);
        return ApiResponse.succeed(vo);
    }

    @Operation(summary = "生产经营-煤炭销售-煤炭运输情况", description = "生产经营-煤炭销售-煤炭运输情况")
    @GetMapping(value = "/coalproduct/operation/sales/transport")
    public ApiResponse<List<CoalTransportVO>> getSalesTransport(@RequestParam(value = "year", required = false) Integer year) {
        // 调用Service方法获取煤炭运输情况数据
        List<CoalTransportVO> resultList = coalProdoperTransportService.getSalesTransport(year);
        return ApiResponse.succeed(resultList);
    }

    @Operation(summary = "生产经营-煤炭销售-销售结构", description = "生产经营-煤炭销售-销售结构")
    @GetMapping(value = "/coalproduct/operation/sales/structure")
    public ApiResponse<CoalSalesStructureVO> getSalesStructure(@RequestParam(value = "year", required = false) Integer year) {
        // 调用Service方法获取煤炭销售结构数据
        CoalSalesStructureVO vo = coalProdoperSalesStructureService.getSalesStructure(year);
        return ApiResponse.succeed(vo);
    }

    @Operation(summary = "产业指标-产业顶部指标", description = "产业指标-产业顶部指标")
    @GetMapping(value = "/coalproduct/top-indicators")
    public ApiResponse<CoalTopIndicatorsVO> getTopIndicators(@RequestParam(value = "year", required = false) Integer year) {
        // 调用Service方法获取煤炭产业顶部指标数据
        CoalTopIndicatorsVO vo = coalOverviewItemService.getTopIndicators(year);
        return ApiResponse.succeed(vo);
    }

    @Operation(summary = "运行监测-设备状态监测-设备动用统计", description = "运行监测-设备状态监测-设备动用统计")
    @GetMapping(value = "/coalproduct/monitoring/equipment/usage")
    public ApiResponse<List<CoalEquipmentUsageVO>> getEquipmentUsage(@RequestParam(value = "year", required = false) Integer year) {
        // 调用Service方法获取设备动用统计数据
        List<CoalEquipmentUsageVO> resultList = coalOpermonEquipUseService.getEquipmentUsage(year);
        return ApiResponse.succeed(resultList);
    }

    @Operation(summary = "运行监测-设备状态监测-设备故障统计", description = "运行监测-设备状态监测-设备故障统计")
    @GetMapping(value = "/coalproduct/monitoring/equipment/failures")
    public ApiResponse<CoalEquipmentFaultVO> getEquipmentFaultStatistics(@RequestParam(value = "year", required = false) Integer year) {
        // 调用Service方法获取设备故障统计数据
        CoalEquipmentFaultVO vo = coalOpermonEquipFaultService.getEquipmentFaultStatistics(year);
        return ApiResponse.succeed(vo);
    }

    @Operation(summary = "运行监测-矿场运行监测-产出情况", description = "运行监测-矿场运行监测-产出情况")
    @GetMapping(value = "/coalproduct/monitoring/production")
    public ApiResponse<List<CoalProductionOutputVO>> getProductionOutput(@RequestParam(value = "year", required = false) Integer year) {
        // 调用Service方法获取产出情况数据
        List<CoalProductionOutputVO> resultList = coalOpermonRtOutputService.getProductionOutput(year);
        return ApiResponse.succeed(resultList);
    }

    @Operation(summary = "运行监测-矿场运行监测-生产效能", description = "运行监测-矿场运行监测-生产效能")
    @GetMapping(value = "/coalproduct/monitoring/efficiency")
    public ApiResponse<List<CoalProductionEfficiencyVO>> getProductionEfficiency(@RequestParam(value = "year", required = false) Integer year) {
        // 调用Service方法获取生产效能数据
        List<CoalProductionEfficiencyVO> resultList = coalOpermonRtEffectService.getProductionEfficiency(year);
        return ApiResponse.succeed(resultList);
    }

    @Operation(summary = "运行监测-矿场运行监测-边坡告警情况", description = "运行监测-矿场运行监测-边坡告警情况")
    @GetMapping(value = "/coalproduct/monitoring/slope-alerts")
    public ApiResponse<List<CoalSlopeAlertVO>> getSlopeAlerts(@RequestParam(value = "year", required = false) Integer year) {
        // 调用Service获取边坡告警数据
        List<CoalSlopeAlertVO> resultList = coalOpermonRtSlopeAlarmService.getSlopeAlerts(year);
        return ApiResponse.succeed(resultList);
    }

    @Operation(summary = "安全环保-生态修复治理面积", description = "安全环保-生态修复治理面积")
    @GetMapping(value = "/coalproduct/safety/eco-restoration")
    public ApiResponse<List<CoalEcoRestorationVO>> getEcoRestoration(@RequestParam(value = "year", required = false) Integer year) {
        // 调用Service方法获取生态修复治理面积数据
        List<CoalEcoRestorationVO> resultList = coalSafetyenvRestorationService.getEcoRestoration(year);
        return ApiResponse.succeed(resultList);
    }

    @Operation(summary = "安全环保-危险废弃物处置情况", description = "安全环保-危险废弃物处置情况")
    @GetMapping(value = "/coalproduct/safety/waste-disposal")
    public ApiResponse<List<CoalWasteDisposalVO>> getWasteDisposal(@RequestParam(value = "year", required = false) Integer year) {
        // 调用Service方法获取危险废弃物处置情况数据
        List<CoalWasteDisposalVO> resultList = coalSafetyenvRiskDealService.getWasteDisposal(year);
        return ApiResponse.succeed(resultList);
    }

    @Operation(summary = "安全环保-安全告警", description = "安全环保-安全告警")
    @GetMapping(value = "/coalproduct/safety/alerts")
    public ApiResponse<List<CoalSafetyAlertVO>> getSafetyAlerts(@RequestParam(value = "year", required = false) Integer year) {
        // 调用Service方法获取安全告警数据
        List<CoalSafetyAlertVO> resultList = coalSafetyenvAlarmService.getSafetyAlerts(year);
        return ApiResponse.succeed(resultList);
    }

    @Operation(summary = "矿区简介", description = "矿区简介")
    @GetMapping(value = "/coalproduct/mine/intro")
    public ApiResponse<CoalMineIntroVO> getMineIntro(@RequestParam(value = "unitCode", required = false) String unitCode) {
        // Mock数据
        CoalMineIntroVO vo = new CoalMineIntroVO();
        
        // 根据unitCode设置矿名
        String mineName = "北露天煤矿"; // 默认值
        if (unitCode != null) {
            switch (unitCode) {
                case "C_01_01":
                    mineName = "南露天矿";
                    break;
                case "C_01_02":
                    mineName = "北露天矿";
                    break;
                case "C_01_03":
                    mineName = "白音华2号矿";
                    break;
                case "C_01_04":
                    mineName = "白音华3号矿";
                    break;
                case "C_01_05":
                    mineName = "扎哈淖尔矿";
                    break;
                default:
                    mineName = "北露天煤矿";
                    break;
            }
        }
        
        vo.setMineName(mineName);
        vo.setImageUrl("/images/mine/beitianmei.jpg");
        vo.setIntroduction("北露天煤矿位于内蒙古自治区呼伦贝尔市鄂温克族自治旗境内，隶属于1985年，煤炭生产能力31000万吨/年。煤炭可采储量为4.24亿吨，煤种为褐煤，原煤平均发热量为1730大卡，是国家级安全生产标准化工程，剥离效率达100%，连续多年评为国家安全生产标准化一级露天煤矿，是国家级绿色矿山，建成标准化露天煤矿的典范案例，以创新驱动，建设标志性工程，是企业发展的方向，以低碳化、清洁化、智慧化为核心方向，坚持创新驱动，发展绿色强健，发展循环经济！");
        vo.setFoundedYear("1985年");
        vo.setAnnualCapacity("31000万吨/年");
        vo.setCoalReserves("4.24亿吨");
        return ApiResponse.succeed(vo);
    }

    @Operation(summary = "煤矿基本信息", description = "煤矿基本信息")
    @GetMapping(value = "/coalproduct/mine/basic-info")
    public ApiResponse<CoalBasicInfoVO> getBasicInfo(@RequestParam(value = "unitCode", required = false) String unitCode) {
        // Mock数据
        CoalBasicInfoVO vo = new CoalBasicInfoVO();
        vo.setDailyCoalOutput(new BigDecimal("124973.60"));
        vo.setDailyCoalSales(new BigDecimal("1653387.80"));
        vo.setDailyOwnStripping(new BigDecimal("2614971.00"));
        vo.setDailyOutsourcedStripping(new BigDecimal("4355670.00"));
        vo.setDailyStrippingRatio(new BigDecimal("4.26"));
        vo.setInventory(new BigDecimal("69.70"));
        return ApiResponse.succeed(vo);
    }

    @Operation(summary = "煤炭运输详情", description = "煤炭运输详情")
    @GetMapping(value = "/coalproduct/transport/detail")
    public ApiResponse<CoalTransportDetailVO> getTransportDetail(@RequestParam(value = "unitCode", required = false) String unitCode) {
        // Mock数据
        CoalTransportDetailVO vo = new CoalTransportDetailVO();
        vo.setLowHeatTransport(new BigDecimal("7860"));
        vo.setNormalHeatTransport(new BigDecimal("20987"));
        vo.setStandardHeatTransport(new BigDecimal("10728"));
        vo.setPremiumHeatTransport(new BigDecimal("9829"));
        
        // 构造趋势数据
        List<CoalTransportDetailVO.TransportTrendItem> trendData = new ArrayList<>();
        for (int i = 1; i <= 31; i++) {
            CoalTransportDetailVO.TransportTrendItem item = new CoalTransportDetailVO.TransportTrendItem();
            item.setDay(i);
            item.setTruckTransport(new BigDecimal(String.valueOf(3000 + (int)(Math.random() * 2000))));
            item.setBeltTransport(new BigDecimal(String.valueOf(5000 + (int)(Math.random() * 3000))));
            item.setRailTransport(new BigDecimal(String.valueOf(4000 + (int)(Math.random() * 4000))));
            trendData.add(item);
        }
        vo.setTrendData(trendData);
        
        return ApiResponse.succeed(vo);
    }

    @Operation(summary = "坑下人员情况", description = "坑下人员情况")
    @GetMapping(value = "/coalproduct/underground/personnel")
    public ApiResponse<CoalUndergroundPersonnelVO> getUndergroundPersonnel(@RequestParam(value = "unitCode", required = false) String unitCode) {
        // Mock数据
        CoalUndergroundPersonnelVO vo = new CoalUndergroundPersonnelVO();
        vo.setPersonnelCount(675);
        vo.setStatisticsTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        
        // 构造人员名单
        List<CoalUndergroundPersonnelVO.PersonnelItem> personnelList = new ArrayList<>();
        
        String[][] personnel = {
            {"张丹", "赵罗", "孙哈萍", "李功河", "肖月"},
            {"刘峰生", "王明明", "段伟文", "吴科", "许子涵"},
            {"张同剑", "白雪", "王何", "曹辉", "秦启贝"},
            {"王永光", "苏慧", "陈景培", "周伟明", "于好刚"},
            {"王志远", "黄雅婷", "郭文清", "沈浩宇", "柯思远"}
        };
        
        String[] teams = {"一班", "二班", "三班", "四班", "五班"};
        
        for (int i = 0; i < personnel.length; i++) {
            for (String name : personnel[i]) {
                CoalUndergroundPersonnelVO.PersonnelItem item = new CoalUndergroundPersonnelVO.PersonnelItem();
                item.setName(name);
                item.setTeam(teams[i]);
                item.setPosition(i % 3 == 0 ? "采煤工" : (i % 3 == 1 ? "支护工" : "运输工"));
                item.setEntryTime(String.format("%02d:%02d", 6 + i, (int)(Math.random() * 60)));
                personnelList.add(item);
            }
        }
        
        vo.setPersonnelList(personnelList);
        
        return ApiResponse.succeed(vo);
    }

    @Operation(summary = "安全环保告警", description = "安全环保告警")
    @GetMapping(value = "/coalproduct/safety-env/alerts")
    public ApiResponse<CoalSafetyEnvVO> getSafetyEnvAlerts(@RequestParam(value = "unitCode", required = false) String unitCode) {
        // Mock数据
        CoalSafetyEnvVO vo = new CoalSafetyEnvVO();
        
        List<CoalSafetyEnvVO.AlertItem> alertList = new ArrayList<>();
        
        String[][] alerts = {
            {"采场测点", "05-25 13:00", "一级告警", "20mm/s", "采场上方出现边坡位移"},
            {"顶部测点", "05-25 12:00", "一级告警", "20mm/s", "顶部出现边坡位移"},
            {"底部测点", "05-25 11:40", "二级告警", "20mm/s", "南部底部出现边坡位移"},
            {"边帮测点", "05-25 10:45", "二级告警", "20mm/s", "采场上方出现边坡位移"},
            {"采场测点", "05-25 10:00", "三级告警", "20mm/s", "采场上方出现边坡位移"}
        };
        
        for (String[] alert : alerts) {
            CoalSafetyEnvVO.AlertItem item = new CoalSafetyEnvVO.AlertItem();
            item.setLocation(alert[0]);
            item.setAlertTime(alert[1]);
            item.setAlertLevel(alert[2]);
            item.setInstantSpeed(alert[3]);
            item.setSlopeAlertInfo(alert[4]);
            alertList.add(item);
        }
        
        vo.setAlertList(alertList);
        
        return ApiResponse.succeed(vo);
    }

}
